import Common from '../../common/common.js';
import GroupMemberItem from './GroupMemberItem.js';
import BaseList from '../base/BaseList.js';
import TimNet from '../../timnet.js';
import {AddGroupMemberWin} from './GroupWin.js';


const logger = Common.log("GroupMemberList");

function GroupMemberList (groupID) {
    BaseList.call(this);
    this.groupID = groupID;
    this.initData();
}
let prototype = Object.create(BaseList.prototype);
prototype.constructor = GroupMemberList;
GroupMemberList.prototype  = prototype;

//初始化成员数据
GroupMemberList.prototype.initData = function(){
    TimNet.getGroupMemberList(this.groupID,memberList=>{
        let self = this;
        let element = this.element;
        let add = Common.tool.createElement({label:"li",tag:"add",props:{className:"add",innerText:"+"}});
        element.appendChild(add);

        let items = memberList.map(member=>{
            let item = new GroupMemberItem();
            item.updateDataByUserID(member.userID);
            item.setMuteUntil(member.muteUntil);
            item.setRole(member.role);
            return item;
        });
        this.setItems(items);

        //添加按钮
        Common.tool.addHandle(add,"click",()=>{
            let win =  new AddGroupMemberWin();
            win.create();
            win.submit = function(){
                let checkedUserIDList = win.userList.getCheckedUserID();
                for(let i =0; i<checkedUserIDList.length;i++){
                    let userID = checkedUserIDList[i].userID;
                    let item = items.filter(item=>item.data.userID == userID);
                    //存在
                    if(item.length > 0){
                        item = item[0];
                        if(item.isDelete){
                            item.isDelete = true;
                            element.appendChild(item.element);
                        }
                    }else{
                        item = new GroupMemberItem();
                        item.updateDataByUserID(userID);
                        self.pushItem(item);
                    }
                }
                win.cancel();
            }
        });
    });
}



GroupMemberList.prototype.setUserIDList = function(userIDList){
    this.setItems( userIDList.map(t=>new FriendListItem().updateDataByUserID(t)));
}


export default GroupMemberList;
